/*
 * This file is part of KubeSphere Console.
 * Copyright (C) 2019 The KubeSphere Console Authors.
 *
 * KubeSphere Console is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * KubeSphere Console is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with KubeSphere Console.  If not, see <https://www.gnu.org/licenses/>.
 */

import React from 'react'
import { toJS } from 'mobx'
import { observer, inject } from 'mobx-react'
import { Link } from 'react-router-dom'
import { get, isEmpty } from 'lodash'
import { Column, Button } from '@kube-design/components'

import { Image, Card, Text } from 'components/Base'
import ApplicationStore from 'stores/openpitrix/application'
import Store from 'stores/rank/workload'
import { app_logo } from './app-logo'
import styles from './index.scss'

// import { Table } from 'element-react'

@inject('rootStore')
@observer
export default class Applications extends React.Component {
  constructor(props) {
    super(props)
    this.store = new ApplicationStore()
    this.i = 0
    this.app_store = new Store({
      cluster: get(props.match, 'params.cluster'),
      namespaces: get(props.match, 'params.namespace'),
    })
  }

  componentDidMount() {
    this.fetchData()
  }

  get routing() {
    return this.props.rootStore.routing
  }

  fetchData() {
    this.store.fetchList({
      // ...this.props.match.params,
      ...this.props,
      limit: 3,
      // cluster: get(props.match, 'params.cluster'),
      // namespaces: get(props.match, 'params.namespace'),
    })
  }

  handleClickApp = e => {
    const { app } = e.currentTarget.dataset
    // console.log(app)
    // const { workspace, cluster, namespace } = this.props
    return this.routing.push(
      // `/${workspace}/clusters/${cluster}/projects/${namespace}/applications/template/${app}`
      `/system-workspace/clusters/default/projects/default/applications/template/${app}`
    )
  }

  handleImageOnError = e => {
    if (e.target.src !== '/assets/default-app.svg') {
      e.target.src = '/assets/default-app.svg'
    }
  }

  renderExtras() {
    const { workspace, cluster, namespace } = this.props.match.params
    return (
      <Link
        className={styles.more}
        // to={`/${workspace}/clusters/${cluster}/projects/${namespace}/applications/template`}
        to={`/clusters/${cluster}/applications`}
      >
        {t('VIEW_MORE')}
      </Link>
    )
  }

  get logo() {
    const { data } = toJS(this.store.list)
    const num = Object.values(data).length // 3
    for (const logo in app_logo) {
      // console.log(Object.values(data)[this.i].name.slice(0, -7))
      if (logo == Object.values(data)[this.i].name.slice(0, -7)) {
        this.i = (this.i + 1) % num
        return app_logo[logo]
      }

      continue
    }
    return app_logo['redis']
  }

  render() {
    const { data } = toJS(this.store.list)
    // console.log(data)
    const { workspace, cluster, namespace } = this.props
    if (isEmpty(data)) {
      return null
    }

    return (
      <Card className={styles.apps}>
        <div className={styles.header}>
          <div className={styles.title}>{t('INSTALLED_APPS')}</div>
          {/* <p style={{ marginBottom: '3em' }}></p> */}
        </div>

        <table className={styles.TableTitleList} style={{ width: '100%' }}>
          <thead>
            <tr>
              <th className={styles.Tabletitle}>App名称</th>
              <th className={styles.Tabletitle}>运行状态</th>
              <th className={styles.Tabletitle}>版本号</th>
              <th className={styles.Tabletitle}>创建时间</th>
            </tr>
          </thead>
          <tbody className={styles.Content}>
            {data.map(item => (
              <tr key={item.cluster_id}>
                <td className={styles.Content}>
                  <Column
                    className={styles.firstColumn}
                    data-app={item.cluster_id}
                    onClick={this.handleClickApp}
                  >
                    {/* <div
                      // className={styles.title}
                      data-app={item.cluster_id}
                      onClick={this.handleClickApp}
                    > */}
                    {/* <label className={styles.icon}> */}
                    {/* <Image
                          // iconLetter={get(item, 'app.name')}
                          // iconSize={10}
                          // src={get(item, 'app.icon')}
                          src="https://bitnami.com/assets/stacks/redis/img/redis-stack-220x234.png"
                          imgSize={20}
                          onError={this.handleImageOnError}
                        /> */}

                    <img
                      // src="https://bitnami.com/assets/stacks/redis/img/redis-stack-220x234.png"
                      // src = {app_logo[i%num]}
                      src={this.logo}
                      // imgSize={4}
                      onError={this.handleImageOnError}
                    />

                    {/* </label> */}
                    <Text
                      onClick={this.handleClickApp}
                      title={<div className={styles.title}>{item.name}</div>}
                    />
                    {/* </div> */}
                  </Column>
                </td>

                <td className={styles.title}>{item.status}</td>
                <td className={styles.title}>{item.version.name}</td>
                <td className={styles.title}>{item.create_time.slice(0, -10).replace('T',' ')}</td>
              </tr>
            ))}
          </tbody>
        </table>

        <div className={styles.footer}>
          <Link
            // to={`/system-workspace/clusters/default/projects/default/applications/`}
            to={`/clusters/${cluster}/applications`}
          >
            <Button>{t('VIEW_MORE')}</Button>
          </Link>
        </div>
      </Card>
    )
  }
}
